from selenium.common import TimeoutException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as  EC

class BasePage:
    """页面基类"""
    def __init__(self,driver):
        self.driver=driver
    def find_element(self,locator):
        """定位元素"""
        return self.driver.find_element(*locator)
    def send_keys(self,locator,content):
        """输入值"""
        self.find_element(locator).send_keys(content)
    def click(self,locator):
        """点击"""
        self.find_element(locator).click()

    def show_waiting(self, driver, locator, timeout=10, poll_frequency=0.5):
        """
        封装通用显式等待
        """
        try:
            # 等待元素出现并返回
            element = WebDriverWait(
                driver,
                timeout,
                poll_frequency=poll_frequency
            ).until(
                EC.presence_of_element_located(locator),
                message=f"超时：在{timeout}秒内未找到元素 {locator}"
            )
            return element
        except TimeoutException as e:
            # 可以根据需要添加日志记录
            print(f"显式等待异常: {str(e)}")
            return None
    def tap(self,coordinates):
        """触摸"""
        self.driver.tap([coordinates])
